﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Criterion;
using Work.BL.Managers;
using Work.BL.POCOs;
using Work.DAL.Hibernate.Abstract;
using Work.DAL.Hibernate.Entities;
using Work.Tools.ObjectMapper;

namespace Work.BL.Hibernate.Managers
{
    public class HSectionManager : ISectionManager
    {
        #region Fields

        private readonly ICWSessionFactory _sessionFactory;
        private readonly IObjectMapper _objectMapper;

        #endregion

        #region Constructors

        public HSectionManager(ICWSessionFactory sessionFactory, IObjectMapper objectMapper)
        {
            this._sessionFactory = sessionFactory;
            this._objectMapper = objectMapper;
        }

        #endregion

        #region IProjectManager Members

        public List<SectionPOCO> GetSections()
        {
            using (ISession session = this._sessionFactory.CreateSession())
            {
                IList<Section> sections = session.CreateCriteria<Section>().List<Section>();
                return sections
                    .Select(r => this._objectMapper.Map<Section, SectionPOCO>(r))
                    .ToList();
            }
        }

        public SectionPOCO GetSectionById(int sectionId)
        {
            using (ISession session = this._sessionFactory.CreateSession())
            {
                Section section = session.Get<Section>(sectionId);
                return _objectMapper.Map<Section, SectionPOCO>(section);
            }
        }

        public SectionPOCO Insert(SectionPOCO sectionPoco)
        {
            using (ISession session = this._sessionFactory.CreateSession())
            {
                Section section = _objectMapper.Map<SectionPOCO, Section>(sectionPoco);
                session.Save(section);
                session.Flush();
                return _objectMapper.Map<Section, SectionPOCO>(section);
            }
        }

        public List<SectionPOCO> GetSections(int startIndex, int maxCount)
        {
            using (ISession session = this._sessionFactory.CreateSession())
            {
                IList<Section> sections = session.CreateCriteria<Section>()
                    .SetFirstResult(startIndex)
                    .SetMaxResults(maxCount)
                    .List<Section>();
                List<SectionPOCO> list = sections
                    .Select(r => this._objectMapper.Map<Section, SectionPOCO>(r))
                    .ToList();
                return list;
            }
        }

        public int GetSectionCount()
        {
            using (ISession session = this._sessionFactory.CreateSession())
            {
                int rowCount = session.CreateCriteria<Section>()
                    .SetProjection(Projections.RowCount()).UniqueResult<int>();
                return rowCount;
            }
        }

        public SectionPOCO Edit(SectionPOCO sectionPoco)
        {
            using (ISession session = this._sessionFactory.CreateSession())
            {
                Section section = _objectMapper.Map<SectionPOCO, Section>(sectionPoco);
                session.Update(section);
                session.Flush();
                return _objectMapper.Map<Section, SectionPOCO>(section);
            }
        }

        public void Delete(SectionPOCO sectionPoco)
        {
            using (ISession session = this._sessionFactory.CreateSession())
            {
                Section section = _objectMapper.Map<SectionPOCO, Section>(sectionPoco);
                session.Delete(section);
                session.Flush();
            }
        }

        #endregion
    }
}
